﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using Model;
using Resources;
using Service.ReferenceData;
using Parameter = Model.Parameter;

public partial class Contacts : System.Web.UI.Page
{
    private readonly DirectServiceSupplier _DirectServiceSupplier = new DirectServiceSupplier();

    private readonly ReferenceDataServiceSupplier _ReferenceDataDirectServiceSupplier =
        new ReferenceDataServiceSupplier();
    private int supplierId = 0; 
    protected void Page_Load(object sender, EventArgs e)
    {

        new Utility().SetRowToDisplay(grdContact, Session["rowsToSDisplay"]);
        DataTable table = new DataTable();
        
       supplierId = GetSupplierId();
       // supplierId = ValueUseOtherPage.companyId; 
       IList list = new ArrayList();
       if(supplierId==0)
       { 
           supplierId = Parameter.contactId;
           txtHiddenTypeId.Text = supplierId.ToString();
           lblEquipmentTypeId.Text = supplierId.ToString();
          _ReferenceDataDirectServiceSupplier.GetSupplierById(supplierId);
       }
       else
       {
          
           txtHiddenTypeId.Text = supplierId.ToString();
           lblEquipmentTypeId.Text = supplierId.ToString();
          _ReferenceDataDirectServiceSupplier.GetSupplierById(supplierId);
           table = _DirectServiceSupplier.GetContactBySupplierId(supplierId.ToString());
           if (table.Rows.Count > 0)
           {
               lblSupplierContact.Visible = true;
           }
           else lblSupplierContact.Visible = false; 

       }

    }
    public int GetSupplierId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["companyId"]) ? Int32.Parse(Request.QueryString["companyId"]) : 0;
    }
    protected void grdContact_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdContact_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            int contactId = Convert.ToInt32(grdContact.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            //_StockService.DeleteSoftware(softwareId);
            _DirectServiceSupplier.DeleteContacts(contactId);
            Parameter.contactId = supplierId;  
            Response.Redirect("Contacts.aspx");
        }
        new Utility().SetSelectedColorToGridView(e, grdContact);
    }
    protected void grdContact_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //block to add message

            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
                deleteButton.Attributes.Add("onclick", "return confirm('" + Resource.ConfirmDeleteMessage + "?');");
            }
        }
    }
}
